package top.csy8888.ch02.demo;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @author CSY
 * @date 2025 - 10 - 10
 */
public class Producer {
    /**
     * 自定义direct类型的交换机的名称
     */
    private static final String EXCHANGE_NAME = "my.direct";

    /**
     * 路由的key
     */
    private static final String ROUTER_KEY = "my.key";

    /**
     * 连接工厂
     */
    private static final ConnectionFactory connectionFactory;

    static {
        connectionFactory = new ConnectionFactory();

        connectionFactory.setHost("127.0.0.1");

        connectionFactory.setPort(5672);

        connectionFactory.setUsername("csy");

        connectionFactory.setPassword("csy11348336");
    }

    public static void send(String message){
        try(Connection conn = connectionFactory.newConnection();
        Channel channel = conn.createChannel()){

            //创建交换机
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

            //投递消息到交换机
            // 参数一：交换机名称
            // 参数二：路由的key
            // 参数三：交换机的其他属性设置
            // 参数四；消息内容（二进制）
            channel.basicPublish(EXCHANGE_NAME, ROUTER_KEY, null, message.getBytes());
        }catch (Exception e){
            throw new RuntimeException("投递失败",e);
        }
    }

    public static void main(String[] args) {
        send("Hello World");
    }
}
